package bookworm;

import java.util.LinkedList;

public class Book 
{
	private LinkedList<Review> reviewsFromABook;
	
	private String bookName;

	private LinkedList<String> bookAuthors;
	private String urlToCover; //url to find book cover image
	
	public Book(String bookName, LinkedList<String> bookAuthors, String urlToCover)
	{
		this.bookName = bookName;
		this.bookAuthors = bookAuthors;
		this.reviewsFromABook = new LinkedList<Review>();
		this.urlToCover = urlToCover;
	}
	
	public int getAverageRatingFromABook()
	{
		int averageRatingOfBook = CalculatesRatingsBookworm.calculateAverageRatingOfBook(this);
		return averageRatingOfBook;
	}

	public LinkedList<Review> getReviewsFromABook() {
		return reviewsFromABook;
	}

	public void setReviewsFromABook(LinkedList<Review> reviewsFromABook) {
		this.reviewsFromABook = reviewsFromABook;
	}
	
	public int getNumberOfReviewsFromBook()
	{
		return this.reviewsFromABook.size();
	}
	
	public String getBookName() {
		return bookName;
	}

	public void setBookName(String bookName) {
		this.bookName = bookName;
	}

	public LinkedList<String> getBookAuthors() {
		return bookAuthors;
	}

	public void setBookAuthors(LinkedList<String> bookAuthors) {
		this.bookAuthors = bookAuthors;
	}
	
	public String getUrlToCover() 
	{
		return urlToCover;
	}
	
	public LinkedList<Review> getGoodRatingReviews()
	{
		LinkedList<Review> goodRatingReviews = new LinkedList<Review>();
		for(int i = 0; i < this.reviewsFromABook.size(); i++)
		{
			Review reviewFromBook = this.reviewsFromABook.get(i);
			int ratingOfReview = reviewFromBook.getRating();
			if(ratingOfReview > 2)
			{
				goodRatingReviews.add(reviewFromBook);
			}
		}
		
		return goodRatingReviews;
	}
	
	public LinkedList<Review> getBadRatingReviews()
	{
		LinkedList<Review> badRatingReviews = new LinkedList<Review>();
		for(int i = 0; i < this.reviewsFromABook.size(); i++)
		{
			Review reviewFromBook = this.reviewsFromABook.get(i);
			int ratingOfReview = reviewFromBook.getRating();
			if(ratingOfReview < 3)
			{
				badRatingReviews.add(reviewFromBook);
			}
		}
		
		return badRatingReviews;
	}
	
	public void addNewReview(Review newReview)
	{
		int newReviewId = newReview.getReviewId();
		boolean thereWasAlreadyThisReviewOnBook = false;
		for(int i = 0; i < this.reviewsFromABook.size(); i++)
		{
			Review oneReview = reviewsFromABook.get(i);
			int oneReviewId = oneReview.getReviewId();
			if(newReviewId == oneReviewId)
			{
				thereWasAlreadyThisReviewOnBook = true;
			}
		}
		
		if(thereWasAlreadyThisReviewOnBook == false)
		{
			this.reviewsFromABook.add(newReview);
		}
	}

}
